<template>
    <v-layout type="main">
        <div class="alike-lis-header">
            <div class="lis-header-left">
                <div class="add-btn" @click="add">+ 新增</div>

                <div class="search-box">
                    <el-input prefix-icon="el-icon-search" v-model="Pagedata.name" class="search" placeholder=""
                        @input="getData" />
                </div>
            </div>
        </div>

        <el-table :data="tableData">
            <el-table-column prop="name" label="名称" />
            <el-table-column prop="eva_image_id" label="评估镜像">、
                <template slot-scope="{ row }">
                    {{ row.eva_image.name }}
                </template>
            </el-table-column>
            <el-table-column prop="test_image_id" label="测试镜像">、
                <template slot-scope="{ row }">
                    {{ row.test_image.name }}
                </template>
            </el-table-column>
            <el-table-column prop="name" label="操作">
                <template slot-scope="{ row }">
                    <el-button type="text" @click="update(row)">修改</el-button>

                    <el-popover placement="top" width="80" trigger="click" v-model="row.visible">
                        <p>您确定要删除吗？</p>
                        <div style="text-align: right; margin: 0">
                            <el-button size="mini" type="text" @click="row.visible = false">取消</el-button>
                            <el-button type="primary" size="mini" @click="del(row)">确定</el-button>
                        </div>
                        <el-button slot="reference" type="text">
                            <i class="el-icon-delete delete-btn" />
                            删除
                        </el-button>
                    </el-popover>

                </template>
            </el-table-column>
        </el-table>

        <el-dialog title="设置" :visible.sync="showAddFormDailog" width="840px">
            <el-form class="form" ref="form" :model="addForm" :rules="submitRules" label-width="124px">
                <el-form-item label="名称" prop="name">
                    <el-input type="text" v-model="addForm.name" />
                </el-form-item>
                <el-form-item label="序号" prop="order">
                    <el-input type="text" v-model="addForm.order" />
                </el-form-item>
                <el-form-item label="评估镜像" prop="eva_image_id">
                    <el-select v-model="addForm.eva_image_id" class="full-extent-select">
                        <el-option v-for="item in imageOptions" :value="item.id" :label="item.name" :key="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="测试镜像" prop="test_image_id">
                    <el-select v-model="addForm.test_image_id" class="full-extent-select">
                        <el-option v-for="item in imageOptions" :value="item.id" :label="item.name" :key="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="commit">确定</el-button>
                    <el-button class="none-type" @click="showAddFormDailog = false">关 闭</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

    </v-layout>
</template>
<script>
import { imageAll } from '@/api/image'
import { getModelEvaluationTypeAll, putModelEvaluationType, addModelEvaluationType, delModelEvaluationType } from '@/api/modelEvaType'
export default {
    data() {
        return {
            tableData: [],
            addForm: {},
            showAddFormDailog: false,
            imageOptions: [],
            submitRules: {
                name: [
                    { required: true, message: "该项为必填项" },
                ],
                order: [
                    { required: true, type: 'number', message: '请输入有效数字', trigger: 'blur', transform: (value) => Number(value) },
                ],
                eva_image_id: [
                    { required: true, message: "该项为必填项" },
                ],
                test_image_id: [
                    { required: true, message: "该项为必填项" },
                ],
            },
            Pagedata: {
                pageNum: 1,
                pageSize: 10,
                total: 0,
                name: "", // 名称搜索
                order: "create_time", // 排序recommend、createTime、likeCount
            },
        };
    },
    async created() {
        this.getData()
        this.getImages()
    },
    methods: {
        async getData() {
            let res = await getModelEvaluationTypeAll({ ...this.Pagedata })
            this.tableData = res.data

        },
        handleCurrentChange() {
            this.getData()
        },
        add() {
            this.showAddFormDailog = true
            this.addForm = {}
        },
        async getImages() {
            let res2 = await imageAll({ visibility: 2, type: 2 })
            this.imageOptions = res2.data
        },
        async del(data) {
            await delModelEvaluationType(data)
            this.getData()
        },
        update(data) {
            this.addForm = JSON.parse(JSON.stringify(data))
            this.showAddFormDailog = true

        },
        async commit() {
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    if (this.addForm.id) {
                        await putModelEvaluationType(this.addForm)
                    }
                    else {
                        await addModelEvaluationType(this.addForm)
                    }
                    this.showAddFormDailog = false
                    this.getData()
                }
            })
        }
    },
}
</script>
<style lang="less" scoped></style>